/* =============================================================================
#     FileName: wl_task.c
#     Desc:
#     LastChange:
#     History:
============================================================================= */
#include "FreeRTOS.h"
#include "cmsis_os.h"
#include "task.h"
#include "wl_task.h"
#include "bsp.h"
#include "stdio.h"

#define TASK_STACK_HUGE_SIZE       2048
#define TASK_STACK_LARGE_SIZE      1024
#define TASK_STACK_MIDDLE_SIZE     512
#define TASK_STACK_SMALL_SIZE      256


portTASK_FUNCTION( commTaskEntry, pvParameters );
portTASK_FUNCTION( displayTaskEntry, pvParameters );
portTASK_FUNCTION( modeTaskEntry, pvParameters );
portTASK_FUNCTION( bmsTaskEntry, pvParameters );
// portTASK_FUNCTION( SubGTaskEntry, pvParameters );
portTASK_FUNCTION( FacTaskEntry, pvParameters );
#ifdef PAYLOAD_UART
portTASK_FUNCTION( TcuTaskEntry, pvParameters );
#endif
// portTASK_FUNCTION( SafeEntry, pvParameters );

TaskHandle_t pTaskhandle[WL_TASK_MAX_ID];
const TaskParameters_t stTaskList[WL_TASK_MAX_ID] =
{
    /* the sequence should be the same with the task id sequence defined in rr_task.h */
    {commTaskEntry,      "COMM",       TASK_STACK_LARGE_SIZE,  NULL, osPriorityHigh,         NULL, {{NULL, 0 , 0}}},
    {modeTaskEntry,      "CTRL",       TASK_STACK_LARGE_SIZE,  NULL, osPriorityAboveNormal,  NULL, {{NULL, 0 , 0}}},
    {displayTaskEntry,   "DIS",        TASK_STACK_LARGE_SIZE,  NULL, osPriorityAboveNormal,  NULL, {{NULL, 0 , 0}}},
    // {SubGTaskEntry,      "SUBG",       TASK_STACK_LARGE_SIZE,  NULL, osPriorityHigh,         NULL, {{NULL, 0 , 0}}}, 
    {bmsTaskEntry,       "BMS",        TASK_STACK_LARGE_SIZE,  NULL, osPriorityNormal,       NULL, {{NULL, 0 , 0}}}, 
    {FacTaskEntry,       "FAC",        TASK_STACK_MIDDLE_SIZE, NULL, osPriorityNormal,       NULL, {{NULL, 0 , 0}}},

#ifdef PAYLOAD_UART
    {TcuTaskEntry,       "TCU",        TASK_STACK_MIDDLE_SIZE, NULL, osPriorityNormal,       NULL, {{NULL, 0 , 0}}},
#endif

#if AM_TEST_SAFE    
    {SafeEntry,          "SAFE",       TASK_STACK_MIDDLE_SIZE, NULL, osPriorityHigh,         NULL, {{NULL, 0 , 0}}},
#endif    
}; 

void wl_task_init(void)
{
    int s32Tid;
    int s32Ret;

    for(s32Tid = 0; s32Tid < WL_TASK_MAX_ID; s32Tid++)
    {
        s32Ret = xTaskCreate(stTaskList[s32Tid].pvTaskCode, stTaskList[s32Tid].pcName, stTaskList[s32Tid].usStackDepth, stTaskList[s32Tid].pvParameters, stTaskList[s32Tid].uxPriority, &pTaskhandle[s32Tid]);
        if(pdPASS != s32Ret)
        {
            //DBGI("!!!Create Task %s failed, err=%d\n", stTaskList[s32Tid].pcName, s32Ret);
            printf("create task %s error", stTaskList[s32Tid].pcName);
        }
    }

    #if AM_TEST_SAFE
    extern unsigned int __Check_Sum ;
    if(__Check_Sum != 0 )
        printf("safe fuction is used !\r\n");
    #endif 
}
